package com.zzyl.service.impl;

import com.zzyl.entity.AccraditationRecord;
import com.zzyl.mapper.AccraditationRecordMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.AccraditationRecordService;
import com.zzyl.service.UserRoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RecordVo;
import com.zzyl.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author sjqn
 * @date 2023/11/7
 */
@Service
public class AccraditationRecordServiceImpl implements AccraditationRecordService {


    @Autowired
    private AccraditationRecordMapper accraditationRecordMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 保存操作记录
     * @param recordVo
     */
    @Override
    public void insert(RecordVo recordVo) {

        AccraditationRecord acc = new AccraditationRecord();
        acc.setHandleType(recordVo.getHandleType());
        acc.setAuditStatus(recordVo.getStatus());
        acc.setBussniessId(recordVo.getId());
        acc.setCurrentStep(recordVo.getStep());
        acc.setCreateTime(LocalDateTime.now());
        acc.setOpinion(recordVo.getOption());
        acc.setApproverName(recordVo.getRealName());
        acc.setApproverId(recordVo.getUserId());
        acc.setType(recordVo.getType());

        //当前的审核步骤
        //入住申请
        Long increment = redisTemplate.boundValueOps("RD:" + recordVo.getId().toString()).increment();
        acc.setStepNo(increment);

        //下一个审核人
        if(ObjectUtil.isNotEmpty(recordVo.getNextAssignee()) ){


            acc.setNextStep(recordVo.getNextStep());

            List<UserRoleVo> userRoleVos = userRoleMapper.selectByUserId(recordVo.getNextAssignee());
            UserRoleVo userRoleVo = userRoleVos.get(0);
            acc.setNextApproverId(userRoleVo.getId());
            acc.setNextApproverRole(userRoleVo.getRoleName());
            acc.setNextApprover(userRoleVo.getUserName());
        }

        accraditationRecordMapper.insert(acc);

    }
}
